package com.linkedin.ranker;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.*;

public class DataReader {

	/*
	 * "SENIORITY_2_NAME"	"CURR_PAGE_KEY_SK"	"COUNT(0)"
"VP"	1	4413
"Training"	25	5289

vp/training/entry/manager/senior/cxo/owner/director/
	 */
	
	public boolean isParsableToInt(String i)
	{
		try
		{
			Integer.parseInt(i);
			return true;
		}
		catch(NumberFormatException nfe)
		{
			return false;
		}
	}
	
	public String getSeniority(int pageKey)
	{
		StringBuilder contents = new StringBuilder();
		final String aFile="/Users/anagpal/seniority.txt";



		//Healthcare Services,Information Technology,Human Resources
		int data[]={20,20,15,10,0,10,0,0};

		try {
			//use buffering, reading one line at a time

			BufferedReader input =  new BufferedReader(new FileReader(aFile));
			int linecount=0;

			/*
			 * readLine is a bit quirky :
			 * it returns the content of a line MINUS the newline.

			 */
			String line="";
			//contents.append(line);
			while (( line = input.readLine()) != null){
				if (linecount==0)
				{
					linecount++;
					continue;
				}
				System.out.println(line);
				StringTokenizer st = new StringTokenizer(line);
				while(st.hasMoreTokens()) {
					String position = st.nextToken();

					String pageKeySK = st.nextToken(); 
					while(!isParsableToInt(pageKeySK))
					{
						pageKeySK = st.nextToken();
					}
					String count = st.nextToken();
					int value= new Integer(count).intValue()/1000;
					int pagevalue= new Integer(pageKeySK).intValue();

					if(value==pageKey)
					{
						if (position.indexOf("VP")>0)
						{
							data[0]=value;

						}
						if (position.indexOf("Training")>0)
						{

							data[1]=value;

						}
						if (position.indexOf("Entry")>0)
						{
							data[2]=value;

						}
						if (position.indexOf("Manager")>0)
						{
							data[3]=value;

						}
						if (position.indexOf("Senior")>0)
						{
							data[4]=value;

						}
						if (position.indexOf("CXO")>0)
						{
							data[5]=value;

						}
						if (position.indexOf("Owner")>0)
						{
							data[6]=value;

						}
						if (position.indexOf("Director")>0)
						{
							data[7]=value;

						}
					}
				}

			}

			input.close();

		}
		catch (IOException ex){
			ex.printStackTrace();
		}

		String dataStr="[";
		
		for(int i=0;i<data.length;i++)
		{
			double datdouble=data[i]/100.00;
			dataStr=dataStr+datdouble+",";
		}
		dataStr=dataStr.substring(0,dataStr.length()-2);
		dataStr+="]";
		return dataStr;
	}

	public String getFunction(int pageKey)
	{
		StringBuilder contents = new StringBuilder();
		final String aFile="/Users/anagpal/function.txt";



		//Healthcare Services,Information Technology,Human Resources
		int data[]={20,20,15,10,0,10,0,0};

		try {
			//use buffering, reading one line at a time

			BufferedReader input =  new BufferedReader(new FileReader(aFile));
			int linecount=0;

			/*
			 * readLine is a bit quirky :
			 * it returns the content of a line MINUS the newline.

			 */
			String line="";
			//contents.append(line);
			while (( line = input.readLine()) != null){
				if (linecount==0)
				{
					linecount++;
					continue;
				}
				System.out.println(line);
				StringTokenizer st = new StringTokenizer(line);
				while(st.hasMoreTokens()) {
					String position = st.nextToken();

					String pageKeySK = st.nextToken(); 
					while(!isParsableToInt(pageKeySK))
					{
						pageKeySK = st.nextToken();
					}
					String count = st.nextToken();
					int value= new Integer(count).intValue()/1000;
					int pagevalue= new Integer(pageKeySK).intValue();

					if(value==pageKey)
					{
						if (position.indexOf("Healthcare ")>0)
						{
							data[0]=value;

						}
						if (position.indexOf("Information ")>0)
						{

							data[1]=value;

						}
						if (position.indexOf("Human ")>0)
						{
							data[2]=value;

						}
						if (position.indexOf("Research")>0)
						{
							data[3]=value;

						}
						if (position.indexOf("Accounting")>0)
						{
							data[4]=value;

						}
						if (position.indexOf("Legal")>0)
						{
							data[5]=value;

						}
						if (position.indexOf("Support")>0)
						{
							data[6]=value;

						}
						if (position.indexOf("Media ")>0)
						{
							data[7]=value;

						}
					}

				}

			}

			input.close();

		}
		catch (IOException ex){
			ex.printStackTrace();
		}

		String dataStr="[";
		
		for(int i=0;i<data.length;i++)
		{
			double datdouble=(data[i]/100.00)+0.1;
			dataStr=dataStr+datdouble+",";
		}
		dataStr=dataStr.substring(0,dataStr.length()-2);
		dataStr+="]";
		return dataStr;
	}

}
